package ee.ut.math.chess.hadoop.dummy;

import org.apache.hadoop.io.BytesWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import ee.ut.math.chess.base.model.Move;
import ee.ut.math.chess.base.model.exception.GameLogicException;
import ee.ut.math.chess.game.model.Game;

public class DummyReduce extends Reducer<Text, BytesWritable, Text, LongWritable>{
    
    protected void reduce(Text key, Iterable<BytesWritable> values, Context context) throws java.io.IOException ,InterruptedException {
        //DUMMY IMPLEMENTATION
        BytesWritable gameInBytes = values.iterator().next();
        Game game;
        try {
            game = DummyHadoopManager.getGameFromBytes(gameInBytes.getBytes());
            Move move = new Move(key.toString());
            game.move(move);
        } catch (ClassNotFoundException e) {
            return;
        } catch (GameLogicException e) {
            return;
        }
        context.write(key, new LongWritable((long)(Math.random()*1000)));
    }
}
